package ru.arriah.servicedesk.web.admin.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.ClientBean;
import ru.arriah.servicedesk.bean.IntWrapper;
import ru.arriah.servicedesk.ejb.ClientManagerLocalHome;
import ru.arriah.servicedesk.ejb.ClientManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class RestoreClientAction extends DumbAction {
   protected ClientManagerLocalHome clientManagerHome;

   public RestoreClientAction() throws NamingException {
      super();

      clientManagerHome =
         Utils.getClientManagerHomeInterface();
   }
   

   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
     try {
              
       int clientId = idVal(request.getParameter("client_id"));
       
       ClientManagerLocalObject clientManager = 
         clientManagerHome.create();
       
       if(clientManager.restoreClient(clientId)) {
          
         addInfMessage(
           request,
           config.getProperty("INF.CLIENT.RESTORED")           
         );
         
       } else
          
         addErrorMessage(
            request,
            config.getProperty("ERROR.CLIENT.NOT_RESTORED")
         );
       
       
       //Prepare clients List
       int page = idVal(request.getParameter("page"));
       int itemsPerPage = 
         Integer.parseInt(config.getProperty("ITEMS_PER_PAGE"));
       
       IntWrapper records = new IntWrapper(0);

       String status;
       String order = request.getParameter("order");
       String orderBy = request.getParameter("orderBy");
       String type = request.getParameter("type");
                   
       order = (order == null) ? "" : order;                
       orderBy = (orderBy == null) ? "" : orderBy;
       type = (type == null) ? "" : type;
       
       if (type.equals("all")) {
         status = "'active', 'inactive'";
       } else if(type.equals("active")) {
         status = "'active'";
       } else if(type.equals("inactive")) {
         status = "'inactive'";
       } else 
         status = "'active'";       
       
       Collection<ClientBean> clientsList = 
         clientManager.selectClients(
           orderBy, 
           order,
           page,
           itemsPerPage,
           records,
           status
         );
       
       clientManager.remove();
       
       this.pageNavigationToRequest(
         page,          
         records.getValue(), 
         "adminui?_action=clientsList&orderBy="+orderBy+"&order="+order+"&type="+type+"&page=",
         request
       );

       request.setAttribute("order", (order.equals("ASC") ? "DESC" : "ASC"));
       request.setAttribute("type", type);
       request.setAttribute("clientsList", clientsList);
       
       return actionMapping.getSuccessTarget();
    } catch (Exception e) {
       handleException(e, "execute");
       throw new InternalException("ErrorMessage.internal");
    }
   }
}